package com.my.mymafengwo.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.smallstrong.mafengwopro.R;
import com.my.mymafengwo.base.WithMediaPlayerActivity;
import com.my.mymafengwo.utils.SharedPrefUtils;

public class WelcomeActivity extends WithMediaPlayerActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    /**
     * 视频播放控件
     */
    private SurfaceView surfaceView;
    /**
     * 跳转到下一个Activity
     */
    private TextView tvNext;
    private CheckBox cbMusic;
    private boolean isFirst;


    @Override
    protected void loadXml() {
        if (SharedPrefUtils.getBoolean(this, SharedPrefUtils.ISFIRST, true)) {
            setContentView(R.layout.activity_welcome);
            isFirst = true;
        } else {
            intoNextActivity();
            isFirst = false;
        }
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {
    }

    @Override
    protected void registerListener() {
        if (tvNext != null)
            tvNext.setOnClickListener(this);
        if (cbMusic != null)
            cbMusic.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initData() {
        if (isFirst) {
            playMedia(surfaceView, R.raw.welcome);
        }
    }

    @Override
    protected void initViews() {
        surfaceView = (SurfaceView) findViewById(R.id.sv_welcome);
        tvNext = (TextView) findViewById(R.id.tv_welcome_next);
        cbMusic = (CheckBox) findViewById(R.id.cb_welcome_music);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_welcome_next:
                stopMedia();
                intoNextActivity();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        super.onCompletion(mp);
        intoNextActivity();
    }

    /**
     * 跳转下一个Activity
     */

    private void intoNextActivity() {
        if (isFirst) {
            SharedPrefUtils.saveBoolean(this, SharedPrefUtils.ISFIRST, false);
        }
        SharedPrefUtils.saveBoolean(this, SharedPrefUtils.ISLOGIN, false);
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    /**
     * 是否静音的监听
     *
     * @param buttonView
     * @param isChecked  true:有声音,false:没声音
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            turnMusic(1.0f, 1.0f);
        } else {
            turnMusic(0, 0);
        }
    }
}
